<template>
  <el-form
    v-if="config.length>0"
    ref="filterForm"
    :inline="true"
    :model="filterModel"
    size="small"
    class="filter-container"
    label-position="left"
    label-width="25%"
    @submit.native.prevent
  >
    <div class="flex-form-item">
      <el-form-item
        v-for="(item,index) in config"
        :key="index"
        :label="item.label"
        :prop="item.prop"
      >
        <!--输入框表单类型-->
        <el-input
          v-if="item.type ==='text'"
          v-model="filterData[item.prop]"
          :placeholder="item.placeholder?item.placeholder:item.label"
          @keyup.enter.native="handleFilter"
        />
        <!--checkbox表单类型-->
        <el-checkbox-group
          v-if="item.type === 'checkbox'"
          v-model="filterData[item.prop]"
          :placeholder="item.placeholder?item.placeholder:'请选择'"
        >
          <el-checkbox
            v-for="option in item.data"
            :key="option.id"
            :label="option.id"
            @change="handleFilter"
          >{{ option.name }}</el-checkbox>
        </el-checkbox-group>
        <!--下拉选择类型-->
        <el-select
          v-if="item.type === 'select'"
          v-model="filterData[item.prop]"
          :multiple="item.multiple"
          collapse-tags
          clearable
          :placeholder="item.placeholder?item.placeholder:'请选择'"
          @change="handleFilter"
        >
          <el-option
            v-for="option in item.data"
            :key="option.id"
            :label="option.name"
            :value="option.id"
          />
        </el-select>
        <!--日期时间类型-->
        <el-date-picker
          v-if="item.type === 'daterange'"
          v-model="filterData[item.prop]"
          type="daterange"
          align="right"
          unlink-panels
          :range-placeholder="item.placeholder && item.placeholder.range?item.placeholder.range:'至'"
          :start-placeholder="item.placeholder && item.placeholder.start?item.placeholder.start:'开始日期'"
          :end-placeholder="item.placeholder && item.placeholder.end?item.placeholder.end:'结束日期'"
          :picker-options="item.pickerOptions"
          @change="handleFilter"
        />
      </el-form-item>
    </div>
    <div class="flex-form-item">
      <el-form-item label=" ">
        <el-button
          v-waves
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >查询</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>

<script>
export default {
  name: 'FilterData',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    config: {
      type: Array,
      default() {
        return []
      }
    },
    filterData: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  data() {
    return {
      filterModel: {}
    }
  },
  watch: {
    /* 实现表单数据的绑定，实时接收父组件的数据变化*/
    filterData() {
      this.filterModel = this.filterData
    }
  },
  mounted() {
    // 将组件上的属性赋值给当前组件内变量，因为props只能单向绑定,还需要监听属性值变化进行父子组件间交互
    this.filterModel = this.filterData
  },
  methods: {
    // 提交表单数据
    handleFilter() {
      this.$emit('filter', this.filterModel)
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  padding: 15px;
  margin-bottom: 10px;
  background-color: #f5f7fd;
  border-radius: 5px;
  .el-input {
    width: 100% !important;
  }

  .el-select {
    width: 100% !important;
  }

  ::v-deep .el-form-item {
    display: flex;
    margin: 0;
    &__label {
      font-size: 12px;
    }
    &__content {
      flex: 1;
    }
    & + .el-form-item {
      margin: 0;
    }
  }
}

.flex-form-item {
  display: grid;
  gap: 15px 2%;
  grid-template-columns: repeat(3, 32%);
  & + .flex-form-item {
    margin-top: 15px;
  }
}
</style>
